# coding=utf-8 
#!/usr/bin/env python

##-----
## Module for handling enums: key value pairs that where only the key needs to be stored in the DB 
##-----
'''
Enums are dictionaries of tuples
index : (short_id, long_id, active, [category1, category2])

'''
authorities = {
                  10:('20 system_owner', 'System owner with rights to do everything', True, ['system']),
                  30:('30 owner', '',True, ['default','group']),
                  40:('40 admin', '',True, ['group']),
                  50:('50 author', '', True, ['group']),
                  60:('60 member', 'member of group', True, ['group']),
                  80:('authenticated', '', True, ['system']),
                  100:('anonymous', '', True, ['system'])
                  }
## ----- Levels
## global owner 10
## global admin 15
## global author 20
## global member 25
## owner 30
## admin 40
## author 50
## member 60                 
## What authority do you need to do something
operations = {
                normal_r:(0,'Normal reader within group',True,[]) ,
                normal_w:(60,'Normal reader/writer within group',True,[]) ,
                author_r:(-1,'Normal reader',False,[]) , 
                author_w:(0,'Normal reader',True,[]) ,
                user_w:(0,'Normal reader',True,[]) ,
                user_r:(0,'Normal reader',True,[]) ,
                admin_r:(0,'Normal reader',True,[]) ,
                admin_w: (0,'Normal reader',True,[]) ,               
                global_r:(0,'Normal reader',True,[]) ,
                global_w:(0,'Normal reader',True,[]) ,
                global_author_r:(0,'Normal reader',True,[]) ,
                global_author_w:(0,'Normal reader',True,[]) ,
                global_user_r:(0,'Normal reader',True,[]) ,
                global_user_w:(0,'Normal reader',True,[]) ,
                global_group_r:(0,'Normal reader',True,[]) ,
                global_group_w:(0,'Normal reader',True,[]) ,
                global_admin_r:(0,'Normal reader',True,[]) ,
                global_admin_w:(0,'Normal reader',True,[]) ,
                global_owner:(0,'Normal reader',True,[]) ,
             }

def index2text(dict,index,long=False):
    '''Given the index number (that is stored in the db) of an enum_dict,
    returns the short text provide is 'active'. When length = 'long' then
    returns the long text'''
    ret = None
    i = int(long) 
    for k in dict.keys():
        if k == index and dict[k][2]:
            ret = dict[k][i]
    return ret

def text2index(dict,text,long=False):
    '''Given text  of an enum_dict, returns the number (that is stored in the
    db) provide that enum is 'active'. When length = 'long' then searches for
    the long text'''
    ret = None
    i = int(long)
    for k in dict.keys():
        if text == dict[k][i] and dict[k][2]:
            ret = k
    return ret

def dict_category(dict, categories, long=False):
    '''
    Returns tuples (index, short text) representing each enum that tagged with a
    category in the categories list
    '''
    ret = []
    i = int(long)
    for k in dict.keys():
        for category in categories:
            if category in dict[k][3] and dict[k][2]:
                ret.append((k, dict[k][i]))
    ret.sort()
    return ret
            

if __name__ == '__main__':
    print 'starting...'
#    pass
#    print 10, text2index(authorities,'group_member')
#    print 20, text2index(authorities, 'member of group', long=True)
    print 30, dict_category(authorities, ['group'])
    print 'done!'

